package cn.cvs.servlet.sysUser;

import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysRoleService;
import cn.cvs.service.SysUserService;
import cn.cvs.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ToUpdateServlet", urlPatterns = "/sys/user/toUpdate")
public class ToUpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );

        SysRoleService sysRoleService= (SysRoleService) SpringUtil.getBean ( "sysRoleService" );
        SysUserService sysUserService= (SysUserService) SpringUtil.getBean ( "sysUserService" );
        List<SysRole> roleList = sysRoleService.getSysRoleList ( );
        request.setAttribute ( "roleList", roleList );

        SysUser user = sysUserService.getUser (
                request.getParameter ( "userid" ) != null ?
                        Integer.parseInt ( request.getParameter ( "userid" ) ) : 0 );
        request.setAttribute ( "sysUser", user );
        request.getRequestDispatcher ( "/WEB-INF/jsp/sysUser/update.jsp" ).forward ( request, response );
    }
}
